package ru.niir.goodfil.ui.tasks;

import ru.niir.goodfil.audit.client.AuditService;
import ru.niir.goodfil.audit.domain.enums.EnviromentSubsystem;
import ru.niir.goodfil.audit.service.AuditServiceImpl;
import ru.niir.goodfil.audit.service.DefaultConfiguration;
import ru.niir.goodfil.audit.util.Assert;
import ru.niir.goodfil.audit.util.BooleanReference;
import ru.niir.goodfil.db.GoodfilFactory;
import ru.niir.goodfil.db.handlers.Goodfil;
import ru.niir.goodfil.ui.Logs1CBean;

import java.util.TimerTask;

public class C1ValidationTask extends TimerTask {
    private final BooleanReference lastC1ValidationResult;

    public C1ValidationTask(BooleanReference lastC1ValidationResult) {
        this.lastC1ValidationResult = lastC1ValidationResult;
    }

    @Override
    public void run() {
        Goodfil goodfil = GoodfilFactory.getInstance();
        AuditService auditService = new AuditServiceImpl(new DefaultConfiguration());

        Assert.notNull(goodfil);
        Assert.notNull(auditService);

        try {
            boolean result = Logs1CBean.get1CIsRunning(goodfil);

            if (result != lastC1ValidationResult.getValue()) {

                if (result) {
                    auditService.logEnviromentSubsystemFound(EnviromentSubsystem.C1);
                } else {
                    auditService.logEnviromentSubsystemLost(EnviromentSubsystem.C1);
                }

                lastC1ValidationResult.setValue(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
